package com.liusand.service.impl;

import cn.hutool.core.util.DesensitizedUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liusand.common.thread.RedisCacheThread;
import com.liusand.common.thread.UsersThreadLocal;
import com.liusand.model.dto.PatientDto;
import com.liusand.model.pojo.Patient;
import com.liusand.mapper.PatientMapper;
import com.liusand.model.vo.PatientVo;
import com.liusand.service.IPatientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 就诊人 服务实现类
 * </p>
 *
 * @author liusand
 * @since 2023-01-29
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements IPatientService {

    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public List<PatientVo> listById() {
        Integer id = UsersThreadLocal.getId();
        String cache = redisTemplate.opsForValue().get("HEALTH_CACHE:PATIENT_ID_" + id);
        if(StringUtils.isNotEmpty(cache)){
            return JSON.parseArray(cache, PatientVo.class);
        }
        List<PatientVo> list = patientMapper.listById(id);
        for(PatientVo vo : list){
            vo.setCardNumber(DesensitizedUtil.idCardNum(vo.getCardNumber(), 4, 4));
        }

        Thread thread = new Thread(new RedisCacheThread(redisTemplate, "HEALTH_CACHE:PATIENT_ID_" + id, JSON.toJSONString(list)));
        thread.start();

        return list;
    }

    @Override
    public Patient getPatient(String patientId) {
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        Integer id = Integer.parseInt(patientId);
        wrapper.eq(Patient::getId, id);
        Patient patient = patientMapper.selectOne(wrapper);
        return patient;
    }

    @Override
    public Boolean savePatient(String patientId, PatientDto patientDto) {
        Patient newPatient = new Patient();
        BeanUtils.copyProperties(patientDto, newPatient);
        Integer id = UsersThreadLocal.getId();
        newPatient.setUserId(id);
        if(patientId == null){
            LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Patient::getUserId, id);
            Long count = patientMapper.selectCount(wrapper);
            if(count >= 5){
                throw new RuntimeException("添加就诊人异常");
            }
            int insert = patientMapper.insert(newPatient);
            if(insert != 1){
                throw new RuntimeException("添加就诊人异常");
            }
        }else{
            newPatient.setId(Integer.parseInt(patientId));
            int i = patientMapper.updateById(newPatient);
            if(i != 1){
                throw new RuntimeException("修改就诊人失败");
            }
        }
        return true;
    }


}
